package com.firefly.admin.tripartite.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.constant.Constants;
import com.firefly.admin.common.enums.ResultCodeEnum;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.tripartite.entity.FyTripartiteWechatApplet;
import com.firefly.admin.tripartite.entity.FyTripartiteWechatMerchant;
import com.firefly.admin.tripartite.mapper.FyTripartiteWechatAppletMapper;
import com.firefly.admin.tripartite.service.FyTripartiteWechatAppletService;
import com.firefly.admin.tripartite.service.FyTripartiteWechatMerchantService;
import com.firefly.admin.tripartite.vo.WechatAppletQueryVo;
import com.firefly.admin.tripartite.vo.WechatAppletStatusUpdateVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class FyTripartiteWechatAppletServiceImpl implements FyTripartiteWechatAppletService {

    @Autowired
    private FyTripartiteWechatAppletMapper fyTripartiteWechatAppletMapper;
    @Autowired
    private FyTripartiteWechatMerchantService fyTripartiteWechatMerchantService;

    /**
     * 分页查询第三方微信小程序接口
     *
     * @param wechatAppletQueryVo 分页查询参数
     * @return 分页列表
     */
    @Override
    public Result<Page<FyTripartiteWechatApplet>> getWechatAppletList(WechatAppletQueryVo wechatAppletQueryVo) {
        log.info("###########FyTripartiteWechatAppletServiceImpl getWechatAppletList param = {}", JSONObject.toJSONString(wechatAppletQueryVo));
        LambdaQueryWrapper<FyTripartiteWechatApplet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(wechatAppletQueryVo.getAppletName()), FyTripartiteWechatApplet::getAppletName, wechatAppletQueryVo.getAppletName());
        queryWrapper.eq(StringUtils.isNotBlank(wechatAppletQueryVo.getAppId()), FyTripartiteWechatApplet::getAppId, wechatAppletQueryVo.getAppId());
        queryWrapper.eq(FyTripartiteWechatApplet::getDeleteFlag, Constants.DeleteFlag.NORMAL);
        queryWrapper.orderByDesc(FyTripartiteWechatApplet::getCreateTime);
        Page<FyTripartiteWechatApplet> page = new Page<>(wechatAppletQueryVo.getPage(), wechatAppletQueryVo.getLimit());
        Page<FyTripartiteWechatApplet> wechatAppletPage = fyTripartiteWechatAppletMapper.selectPage(page, queryWrapper);
        return Result.success(wechatAppletPage);
    }

    /**
     * 第三方微信小程序信息新增接口
     *
     * @param fyTripartiteWechatApplet 微信小程序信息
     * @return 操作结果
     */
    @Override
    public Result<FyTripartiteWechatApplet> add(FyTripartiteWechatApplet fyTripartiteWechatApplet) {
        if (fyTripartiteWechatApplet == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        ResultCodeEnum resultCodeEnum = checkWechatAppletParam(fyTripartiteWechatApplet);
        if (resultCodeEnum != null) {
            return Result.getResult(resultCodeEnum);
        }
        int rows = fyTripartiteWechatAppletMapper.insert(fyTripartiteWechatApplet);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.ADD_EXCEPTION);
        }
        return Result.success(fyTripartiteWechatApplet);
    }

    /**
     * 第三方微信小程序信息编辑接口
     *
     * @param fyTripartiteWechatApplet 微信小程序信息
     * @return 操作结果
     */
    @Override
    public Result<FyTripartiteWechatApplet> update(FyTripartiteWechatApplet fyTripartiteWechatApplet) {
        if (fyTripartiteWechatApplet == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        Long appletId = fyTripartiteWechatApplet.getAppletId();
        if (appletId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FyTripartiteWechatApplet wechatApplet = fyTripartiteWechatAppletMapper.selectById(appletId);
        if (wechatApplet == null) {
            return Result.getResult(ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_NOT_FOUND);
        }
        ResultCodeEnum resultCodeEnum = checkWechatAppletParam(fyTripartiteWechatApplet);
        if (resultCodeEnum != null) {
            return Result.getResult(resultCodeEnum);
        }
        int rows = fyTripartiteWechatAppletMapper.updateById(fyTripartiteWechatApplet);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.UPDATE_EXCEPTION);
        }
        return Result.success(fyTripartiteWechatApplet);
    }

    /**
     * 第三方微信小程序状态编辑接口
     *
     * @param wechatAppletStatusUpdateVo 状态信息
     * @return 操作结果
     */
    @Override
    public Result<FyTripartiteWechatApplet> updateStatus(WechatAppletStatusUpdateVo wechatAppletStatusUpdateVo) {
        if (wechatAppletStatusUpdateVo == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        Long appletId = wechatAppletStatusUpdateVo.getAppletId();
        if (appletId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FyTripartiteWechatApplet fyTripartiteWechatApplet = fyTripartiteWechatAppletMapper.selectById(appletId);
        if (fyTripartiteWechatApplet == null) {
            return Result.getResult(ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_NOT_FOUND);
        }
        String appletStatus = wechatAppletStatusUpdateVo.getAppletStatus();
        if (StringUtils.isBlank(appletStatus)) {
            return Result.getResult(ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_STATUS_NOT_NULL);
        }
        fyTripartiteWechatApplet.setAppletStatus(appletStatus);
        int rows = fyTripartiteWechatAppletMapper.updateById(fyTripartiteWechatApplet);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.UPDATE_EXCEPTION);
        }
        return Result.success(fyTripartiteWechatApplet);
    }

    /**
     * 第三方微信小程序信息删除接口
     *
     * @param appletId 微信小程序ID
     * @return 操作结果
     */
    @Override
    public Result<String> delete(Long appletId) {
        if (appletId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FyTripartiteWechatApplet fyTripartiteWechatApplet = fyTripartiteWechatAppletMapper.selectById(appletId);
        if (fyTripartiteWechatApplet == null) {
            return Result.getResult(ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_NOT_FOUND);
        }
        fyTripartiteWechatApplet.setDeleteFlag(Constants.DeleteFlag.DELETE);
        int rows = fyTripartiteWechatAppletMapper.updateById(fyTripartiteWechatApplet);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.DELETE_EXCEPTION);
        }
        return Result.success();
    }

    /**
     * 第三方微信小程序APPID校验接口
     *
     * @param appId    微信小程序名称
     * @param appletId 微信小程序ID
     * @return 校验结果
     */
    @Override
    public Result<String> appIdCheck(String appId, Long appletId) {
        if (StringUtils.isBlank(appId)) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        boolean isExistAppId = isExistAppId(appId, appletId);
        if (isExistAppId) {
            return Result.success(ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_APPID_EXIST.getMsg());
        }
        return Result.success();
    }

    /**
     * 根据微信小程序ID查询微信小程序信息
     *
     * @param appletId 微信小程序ID
     * @return 微信小程序信息
     */
    @Override
    public FyTripartiteWechatApplet findById(Long appletId) {
        return fyTripartiteWechatAppletMapper.selectById(appletId);
    }

    public ResultCodeEnum checkWechatAppletParam(FyTripartiteWechatApplet fyTripartiteWechatApplet) {
        Long appletId = fyTripartiteWechatApplet.getAppletId();
        String appletName = fyTripartiteWechatApplet.getAppletName();
        if (StringUtils.isBlank(appletName)) {
            return ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_NAME_NOT_NULL;
        }
        String appId = fyTripartiteWechatApplet.getAppId();
        if (StringUtils.isBlank(appId)) {
            return ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_APPID_NOT_NULL;
        }
        boolean isExistAppId = isExistAppId(appId, appletId);
        if (isExistAppId) {
            return ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_APPID_EXIST;
        }
        String appletSecret = fyTripartiteWechatApplet.getAppletSecret();
        if (StringUtils.isBlank(appletSecret)) {
            return ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_SECRET_NOT_NULL;
        }
        String appletToken = fyTripartiteWechatApplet.getAppletToken();
        if (StringUtils.isBlank(appletToken)) {
            return ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_TOKEN_NOT_NULL;
        }
        Long merchantConfigId = fyTripartiteWechatApplet.getMerchantConfigId();
        if (merchantConfigId != null) {
            FyTripartiteWechatMerchant wechatMerchant = fyTripartiteWechatMerchantService.findById(merchantConfigId);
            if (wechatMerchant == null) {
                return ResultCodeEnum.TRIPARTITE_WECHAT_MERCHANT_NOT_FOUND;
            }
        }
        return null;
    }

    /**
     * 校验小程序APPID是否存在
     *
     * @param appId    小程序appId
     * @param appletId 小程序ID
     * @return true: 存在   false:不存在
     */
    public boolean isExistAppId(String appId, Long appletId) {
        LambdaQueryWrapper<FyTripartiteWechatApplet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FyTripartiteWechatApplet::getAppId, appId);
        queryWrapper.eq(FyTripartiteWechatApplet::getDeleteFlag, Constants.DeleteFlag.NORMAL);
        List<FyTripartiteWechatApplet> wechatAppletList = fyTripartiteWechatAppletMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(wechatAppletList)) {
            if (appletId == null) {
                //新增
                return true;
            } else {
                //编辑
                for (FyTripartiteWechatApplet wechatApplet : wechatAppletList) {
                    if (appletId.longValue() != wechatApplet.getAppletId().longValue()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
